package com.casic.ops.service.impl.pipeline;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.ops.dao.pipeline.PipelineNodeLoggerDAO;
import com.casic.ops.entity.domain.pipeline.PipelineNodeInfoDO;
import com.casic.ops.entity.domain.pipeline.PipelineNodeLoggerDO;
import com.casic.ops.entity.dto.pipeline.PipelineNodeInfoDTO;
import com.casic.ops.entity.dto.pipeline.PipelineNodeLoggerDTO;
import com.casic.ops.entity.dto.pipeline.datastructure.Logger.NodeLogger;
import com.casic.ops.entity.request.pipeline.CommonPageRequest;
import com.casic.ops.entity.request.pipeline.PipelineNodeLoggerRequest;
import com.casic.ops.entity.vo.pipeline.response.MultiResponse;
import com.casic.ops.entity.vo.pipeline.response.PageResponse;
import com.casic.ops.entity.vo.pipeline.response.Response;
import com.casic.ops.entity.vo.pipeline.response.SingleResponse;
import com.casic.ops.service.api.pipeline.PipelineNodeLoggerService;
import com.casic.ops.utils.OpsSnowFlake;
import com.casic.ops.utils.pipeline.PipelineNodeLoggerConvertor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PipelineNodeLoggerServiceImpl implements PipelineNodeLoggerService {
    public PipelineNodeLoggerServiceImpl() {
        super();
    }

    @Resource
    private PipelineNodeLoggerDAO pipelineNodeLoggerDAO;

    @Resource
    private PipelineNodeLoggerConvertor pipelineNodeLoggerConvertor;

    @Override
    public Response add(PipelineNodeLoggerRequest pipelineNodeLoggerRequest) {
        PipelineNodeLoggerDTO pipelineNodeLoggerDTO=pipelineNodeLoggerRequest.getPipelineNodeLoggerDTO();
        pipelineNodeLoggerDTO.init();
        int result=pipelineNodeLoggerDAO.insert(pipelineNodeLoggerConvertor.toDO(pipelineNodeLoggerDTO));
        if (result==0){
            return Response.error("新增失败");
        }else {
            return Response.success();
        }
    }

    @Override
    public Response update(PipelineNodeLoggerRequest pipelineNodeLoggerRequest) {
        int result=pipelineNodeLoggerDAO.updateById(pipelineNodeLoggerConvertor.toDO(pipelineNodeLoggerRequest.getPipelineNodeLoggerDTO()));
        if (result==0){
            return Response.error("更新失败");
        }else{
            return Response.success();
        }
    }

    @Override
    public Response delete(Long[] ids) {
        /**
         * 逻辑删除，0为已删除,1为未删除
         */
        for(Long id:ids){
            PipelineNodeLoggerDO pipelineNodeLoggerDO=pipelineNodeLoggerDAO.selectById(id);
            if (pipelineNodeLoggerDO!=null){
                pipelineNodeLoggerDO.setDeleted("0");
                pipelineNodeLoggerDAO.updateById(pipelineNodeLoggerDO);
            }
        }
        return Response.success();
    }

    @Override
    public SingleResponse<PipelineNodeLoggerDTO> getById(Long id) {
        PipelineNodeLoggerDO pipelineNodeLoggerDO=pipelineNodeLoggerDAO.selectById(id);
        if (pipelineNodeLoggerDO!=null){
            return SingleResponse.of(pipelineNodeLoggerConvertor.toDTO(pipelineNodeLoggerDO));
        }else{
            return null;
        }
    }

    @Override
    public MultiResponse<PipelineNodeLoggerDTO> list() {
        QueryWrapper<PipelineNodeLoggerDO> qw=new QueryWrapper<>();
        List<PipelineNodeLoggerDO> pipelineNodeLoggerDOList=pipelineNodeLoggerDAO.selectList(qw);
        List<PipelineNodeLoggerDTO> pipelineNodeLoggerDTOList=pipelineNodeLoggerConvertor.toDTOList(pipelineNodeLoggerDOList);
        return MultiResponse.of(pipelineNodeLoggerDTOList);
    }

    @Override
    public PageResponse<PipelineNodeLoggerDTO> page(CommonPageRequest request) {
        QueryWrapper<PipelineNodeLoggerDO> qw=new QueryWrapper();
        Page<PipelineNodeLoggerDO> page=new Page(request.getPageIndex(),request.getPageSize());
        IPage<PipelineNodeLoggerDO> iPage=pipelineNodeLoggerDAO.selectPage(page,qw);
        /**
         * 查询总数
         */
        Integer count=pipelineNodeLoggerDAO.selectCount(qw);
        PageResponse<PipelineNodeLoggerDTO> response=new PageResponse<>();
        response.setTotalCount(count);
        List<PipelineNodeLoggerDTO> pipelineNodeLoggerDTOList=pipelineNodeLoggerConvertor.toDTOList(iPage.getRecords());
        response.setData(pipelineNodeLoggerDTOList);
        return response;

    }

    @Override
    public NodeLogger getByLoggerUUIDAndNodeUUID(String loggerUUID, String nodeUUID) {
        QueryWrapper<PipelineNodeLoggerDO> qw=new QueryWrapper<>();
        qw.eq("pipeline_logger_uuid",loggerUUID).eq("node_uuid",nodeUUID);
        PipelineNodeLoggerDO pipelineNodeLoggerDO=pipelineNodeLoggerDAO.selectOne(qw);
        if(pipelineNodeLoggerDO==null){
            return null;
        }
        NodeLogger nodeLogger=new NodeLogger();
        nodeLogger.setLoggerUuid(pipelineNodeLoggerDO.getPipelineLoggerUUid());
        nodeLogger.setNodeUuid(pipelineNodeLoggerDO.getNodeUuid());
        nodeLogger.setDeleted(pipelineNodeLoggerDO.getDeleted());
        nodeLogger.setExecuteStartTime(pipelineNodeLoggerDO.getExecuteStartTime());
        nodeLogger.setLogger(pipelineNodeLoggerDO.getNodeData());
        nodeLogger.setCreator(pipelineNodeLoggerDO.getCreator());
        nodeLogger.setExecuteEndTime(pipelineNodeLoggerDO.getExecuteEndTime());
        nodeLogger.setModifier(pipelineNodeLoggerDO.getModifier());
        nodeLogger.setGmtCreate(pipelineNodeLoggerDO.getGmtCreate());
        nodeLogger.setGmtModified(pipelineNodeLoggerDO.getGmtModified());
        nodeLogger.setUuid(pipelineNodeLoggerDO.getUuid());
        nodeLogger.setId(pipelineNodeLoggerDO.getId());
        return nodeLogger;
    }

    @Override
    public void insertByNodeLogger(NodeLogger nodeLogger) {
        PipelineNodeLoggerDO pipelineNodeLoggerDO=new PipelineNodeLoggerDO();
        pipelineNodeLoggerDO.setNodeData(nodeLogger.getLogger());
        pipelineNodeLoggerDO.setPipelineLoggerUUid(nodeLogger.getLoggerUuid());
        pipelineNodeLoggerDO.setNodeUuid(nodeLogger.getNodeUuid());
        pipelineNodeLoggerDO.setUuid(nodeLogger.getUuid());
        pipelineNodeLoggerDO.setExecuteStartTime(nodeLogger.getExecuteStartTime());
        pipelineNodeLoggerDO.setDeleted(nodeLogger.getDeleted());
        pipelineNodeLoggerDO.setFinalStatus(nodeLogger.getFinalStatus());
        pipelineNodeLoggerDO.setExecuteEndTime(nodeLogger.getExecuteEndTime());
        pipelineNodeLoggerDO.setGmtCreate(nodeLogger.getGmtCreate());
        pipelineNodeLoggerDO.setModifier(nodeLogger.getModifier());
        pipelineNodeLoggerDO.setGmtModified(nodeLogger.getGmtModified());
        pipelineNodeLoggerDO.setUuid(OpsSnowFlake.getInstance().nextId());
        pipelineNodeLoggerDO.setNodeName(nodeLogger.getNodeName());
        pipelineNodeLoggerDAO.insert(pipelineNodeLoggerDO);
    }

    @Override
    public void updateByNodeLogger(NodeLogger nodeLogger) {
//        PipelineNodeLoggerDO pipelineNodeLoggerDO=new PipelineNodeLoggerDO();
//        pipelineNodeLoggerDO.setNodeData(nodeLogger.getLogger());
//        pipelineNodeLoggerDO.setPipelineLoggerUUid(nodeLogger.getLoggerUuid());
//        pipelineNodeLoggerDO.setNodeUuid(nodeLogger.getNodeUuid());
//        pipelineNodeLoggerDO.setUuid(nodeLogger.getUuid());
//        pipelineNodeLoggerDO.setExecuteStartTime(nodeLogger.getExecuteStartTime());
//        pipelineNodeLoggerDO.setDeleted(nodeLogger.getDeleted());
//        pipelineNodeLoggerDO.setFinalStatus(nodeLogger.getFinalStatus());
//        pipelineNodeLoggerDO.setExecuteEndTime(nodeLogger.getExecuteEndTime());
//        pipelineNodeLoggerDO.setGmtCreate(nodeLogger.getGmtCreate());
//        pipelineNodeLoggerDO.setModifier(nodeLogger.getModifier());
//        pipelineNodeLoggerDO.setGmtModified(nodeLogger.getGmtModified());
//        pipelineNodeLoggerDO.setUuid(OpsSnowFlake.getInstance().nextId());
//        pipelineNodeLoggerDO.setNodeName(nodeLogger.getNodeName());
        UpdateWrapper<PipelineNodeLoggerDO> uw=new UpdateWrapper<>();
        //下面的pipeline_logger_uuid要转换成string，不然数据库会爆1292 - Truncated incorrect DOUBLE value:异常
        uw.eq("pipeline_logger_uuid",Long.valueOf(nodeLogger.getLoggerUuid()).toString());
        uw.eq("node_uuid",nodeLogger.getNodeUuid());
        uw.set("final_status",nodeLogger.getFinalStatus());
        uw.set("execute_start_time",nodeLogger.getExecuteStartTime());
        uw.set("execute_end_time",nodeLogger.getExecuteEndTime());
        uw.set("node_data",nodeLogger.getLogger());
        pipelineNodeLoggerDAO.update(null,uw);
    }
}
